local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.CharacterObject(),script.Parent.Name.."_"..script.Name);
local ConnectClass=_G.CommonOHOH.load.Connect();
local ColliderGroup = CommonOHOH.load.ColliderGroup();
local ColliderManager = CommonOHOH.load.ColliderManager();
local LinkCharacter=CommonOHOH:GetPath_Loop("LinkCharacter",game.ReplicatedStorage.Resource)
local IsClient = game:GetService("RunService"):IsClient();
local CallBackManager = CommonOHOH.load.CallBackManager();

local StateMachine=CommonOHOH.load.StateMachine()

function M.new(...)
	return M(...);
end;
--data{
    -- userId=4541733637, --用户Id
    -- level=1,-- 喂养等级
    -- baseLevel=1,--觉醒等级
    -- lock=0
-- }
function M.ctor(self,character,player,guid,data,parent)
	
    self.player=player

    --初始化一个状态机
     self.StateMachine=StateMachine.new()

     --创建状态
    self.StateMachine:CreateState("idle")
    self.StateMachine:CreateState("follow")
    self.StateMachine:CreateState("attack")

    self.DtorEvent=_G.CommonOHOH.load.Connect().new()

    self.StatusEvent=_G.CommonOHOH.load.Connect().new() -- zhuan

    M.super.ctor(self,character);


    if  IsClient then
        -- 添加武器
        -- self:InitWeapon()

        self.guid= character.Name

        local dataStore=self.player:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())
        local equipData=dataStore:GetData("broEquip")
        local id=equipData[self.guid]
        -- print("---PrintTable--broEquip-->----0---",id)
        CommonOHOH:PrintTable(equipData)
        self:UpdateWeapon(id)

        CallBackManager:AddRbxConnect(self,self.player.UserDataEvent, function(obj,data)

            if data["broEquip"] then
                local id=data.broEquip[self.guid]
                self:UpdateWeapon(id)
            end;
        end);


        -- self:AddPrivateComponentByFolder(script.cli)
    else
        character.Name=guid
        self.guid=guid
        self.userId=data.userId
        self.level=data.level
        self.baseLevel=data.baseLevel
        self:SetNetworkOwner(self.player)

        self:InitCharacter(parent)

        -- 添加武器
        -- self:InitWeapon()
        local dataStore=self.player:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent())
        local id=dataStore:GetData("broEquip").__real[self.guid]

        -- print("---PrintTable--broEquip-->-------",id)
        -- CommonOHOH:PrintTable(dataStore:GetData("broEquip").__real)
        self:UpdateWeapon(id)
        
        self:AddPrivateComponentByFolder(script.ser)


        CallBackManager:AddRbxConnect(self,self.player.UserDataEvent, function(obj,key)

            if key =="broEquip" then

                local dataStore=self.player:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent())
                local id=dataStore:GetData("broEquip").__real[self.guid]
                self:UpdateWeapon(id)
            
            end;
        end);

        

    end
    self:AddPrivateComponentByFolder(script.com)

    self.StateMachine:SwitchState("idle")



end


function M.UpdateWeapon(self,wid)

    -- print("-----UpdateWeapon-------",wid)
    -- assert(wid,"wid==nil")
    wid=wid or "1_1"

    if self.Weapon then
        if self.Weapon:IsEqual(wid) then
            return 
        else
            self.Weapon:dtor()
        end
    end
    self.Weapon=_G.CommonOHOH.load.FriendRow().new(wid,self,self.player)
end

--析构函数
function M.dtor(self)
    if self.tagAtt then
        self.tagAtt:SetAttribute("isUsing",false)
    end

    self.DtorEvent:dtor()
    self.StateMachine:dtor()
    local obj=self.main_inst
    M.super.dtor(self);
    obj:Destroy()

    -- print("被卸载了---》")
end;


function M.GetLevel(self)
    return self.level
end
-- 获取状态
function M.GetState(self,id)
    return self.StateMachine:GetState(id)
end


-- 切换状态
function M.SwitchState(self,id)
    self.StateMachine:SwitchState(id)
end

--获取当前状态
function M.GetCurrentState(self)
    self.StateMachine:GetCurrentState()
end


function M.GetWeapon(self)

    return self.Weapon
end
-- function M.InitWeapon(self)
--     self.Weapon=_G.CommonOHOH.load.FriendRow().new("1_1",self,self.player)
-- end


-- 获取跟追踪点
function M.GetTagAtt(self)
    if self.tagAtt then
        return self.tagAtt
    end

   local part= self.player.character:GetRootPart_Loop()

   for i=1,100 do
        local name="FriendFollowAtt_"..i
        local att=part:FindFirstChild(name)
        assert(att,"can not find Att:"..i)

        if not att:GetAttribute("isUsing") then
            self.tagAtt=att
            self.tagAtt:SetAttribute("isUsing",true)
            return self.tagAtt
        end
   end
   
end

-- 初始化角色
function M.InitCharacter(self,parent)
    local att=self:GetTagAtt()
	self:SetPosition(att.WorldCFrame.Position)
	self.main_inst.Parent=parent
	self.main_inst.Humanoid.DisplayDistanceType=Enum.HumanoidDisplayDistanceType.None
    ColliderManager:SetAllGroup(self.main_inst, ColliderGroup.character);
end

--绑定到玩家
function M.Bind2Player(self)
    local att=self:GetTagAtt()
	for _,p in pairs(self.main_inst:GetDescendants()) do
        if p:IsA("BasePart") then
            p.Anchored=false
            p.CanTouch=false
            p.CanCollide=false
            p.Massless=true
        end
    end

	local link=LinkCharacter:Clone()
    link.Parent=self.main_inst
    link.CFrame=att.WorldCFrame:ToWorldSpace(link:FindFirstChild("rootPos").Value)
    link:FindFirstChildOfClass("AlignOrientation").Attachment1=att
    link:FindFirstChildOfClass("AlignPosition").Attachment1=att
    local pp=self.main_inst.PrimaryPart
    local w= Instance.new("WeldConstraint",link)
    w.Part0=pp
    w.Part1=link

end


return M
